package cn.iocoder.an.module.business.controller.admin.repairrequest;

import cn.iocoder.an.module.business.controller.admin.repairtask.vo.RepairTaskRespVO;
import cn.iocoder.an.module.business.dal.dataobject.repairtask.RepairTaskDO;
import cn.iocoder.an.module.business.service.repairtask.RepairTaskService;
import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.repairrequest.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.repairrequest.RepairRequestDO;
import cn.iocoder.an.module.business.service.repairrequest.RepairRequestService;

@Tag(name = "管理后台 - 维修申请")
@RestController
@RequestMapping("/business/repair-request")
@Validated
public class RepairRequestController {

    @Resource
    private RepairRequestService repairRequestService;

    @Resource
    private RepairTaskService repairTaskService;

    @PostMapping("/create")
    @Operation(summary = "创建维修申请")
    @PreAuthorize("@ss.hasPermission('business:repair-request:create')")
    public CommonResult<Long> createRepairRequest(@Valid @RequestBody RepairRequestSaveReqVO createReqVO) {
        return success(repairRequestService.createRepairRequest(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新维修申请")
    @PreAuthorize("@ss.hasPermission('business:repair-request:update')")
    public CommonResult<Boolean> updateRepairRequest(@Valid @RequestBody RepairRequestSaveReqVO updateReqVO) {
        repairRequestService.updateRepairRequest(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除维修申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:repair-request:delete')")
    public CommonResult<Boolean> deleteRepairRequest(@RequestParam("id") Long id) {
        repairRequestService.deleteRepairRequest(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除维修申请")
                @PreAuthorize("@ss.hasPermission('business:repair-request:delete')")
    public CommonResult<Boolean> deleteRepairRequestList(@RequestParam("ids") List<Long> ids) {
        repairRequestService.deleteRepairRequestListByIds(ids);
        return success(true);
    }

    @TransMethodResult
    @GetMapping("/get")
    @Operation(summary = "获得维修申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:repair-request:query')")
    public CommonResult<RepairRequestRespVO> getRepairRequest(@RequestParam("id") Long id) {
        RepairRequestDO repairRequest = repairRequestService.getRepairRequest(id);
        RepairRequestRespVO bean = BeanUtils.toBean(repairRequest, RepairRequestRespVO.class);
        RepairTaskDO repairTaskByRepairId = repairTaskService.getRepairTaskByRepairId(bean.getId());
        RepairTaskRespVO task = BeanUtils.toBean(repairTaskByRepairId, RepairTaskRespVO.class);
        bean.setTaskRespVO(task);
        return success(bean);
    }

    @TransMethodResult
    @GetMapping("/page")
    @Operation(summary = "获得维修申请分页")
    @PreAuthorize("@ss.hasPermission('business:repair-request:query')")
    public CommonResult<PageResult<RepairRequestRespVO>> getRepairRequestPage(@Valid RepairRequestPageReqVO pageReqVO) {
        PageResult<RepairRequestDO> pageResult = repairRequestService.getRepairRequestPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RepairRequestRespVO.class));
    }

    @TransMethodResult
    @GetMapping("/taskPage")
    @Operation(summary = "获得维修任务单分页")
    public CommonResult<PageResult<RepairRequestRespVO>> getRepairRequestTaskPage(@Valid RepairRequestPageReqVO pageReqVO) {
        PageResult<RepairRequestDO> pageResult = repairRequestService.getRepairRequestTaskPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RepairRequestRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出维修申请 Excel")
    @PreAuthorize("@ss.hasPermission('business:repair-request:export')")
    @ApiAccessLog(operateType = EXPORT)
    @TransMethodResult
    public void exportRepairRequestExcel(@Valid RepairRequestPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RepairRequestDO> list = repairRequestService.getRepairRequestPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "维修申请.xls", "数据", RepairRequestRespVO.class,
                        BeanUtils.toBean(list, RepairRequestRespVO.class));
    }

}